package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Professor;
import com.han.bom.Student;
import com.han.dao.UserDAO;

/**
 * Servlet implementation class ViewStudentServlet
 */
@WebServlet("/Professor/AddAndEditStudentServlet")
public class AddAndEditStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private Logger logger = Logger.getLogger(getClass().getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddAndEditStudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		logger.info("Entered AddAndEditStudentServlet POST");
		
		Professor professor = null;
		UserDAO userdao = new UserDAO();
		Student student = null;
		String studentUserID = null,studentFirstName = null,studentLastName = null;
		
		HttpSession session = request.getSession();
		
		if(session.getAttribute("professor")!=null){
			professor = (Professor)session.getAttribute("professor");
		}
		else{
			
		}
		
		if(request.getParameter("studentFirstName")!=null){
			studentFirstName = request.getParameter("studentFirstName");
		}
		else{
			
		}
		
		if(request.getParameter("studentLastName")!=null){
			studentLastName = request.getParameter("studentLastName");
		}
		else{
			
		}
		
		int userID=-1;
		
		if(request.getParameter("studentUserID")!=null){
			studentUserID = request.getParameter("studentUserID");
		}
		else{
			
			try {
				userID = userdao.retrieveMaxUserID();
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		}
		
		if(userID==-1){
			
			student = professor.searchStudent(studentUserID);
			
			if(student!=null){
				
				student.setFirstName(studentFirstName);
				student.setLastName(studentLastName);
				
				userID = Integer.parseInt(student.getUserID().substring(1));
				
				student.setUsername(studentFirstName.trim().charAt(0)+studentLastName.replace(" ","")+userID);
				
				try {
					userdao.updateUser(student);
				} catch (ClassNotFoundException e) {
					logger.error(e.getMessage(), e);
				} catch (SQLException e) {
					logger.error(e.getMessage(), e);
				}
			
				response.sendRedirect("Professor/Student_View.jsp");
			}
			else{
				
			}
		}
		else{
			
			Student newStudent = new Student(userID);
			
			newStudent.setFirstName(studentFirstName);
			newStudent.setLastName(studentLastName);
			newStudent.setUsername(studentFirstName.trim().charAt(0)+studentLastName.replace(" ","")+userID);
			newStudent.setPassword("8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
			
			professor.getStudents().add(newStudent);
			
			try {
				userdao.insertUser(newStudent);
				userdao.addStudentToProfessor(newStudent.getUserID(),professor.getUserID());
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			response.sendRedirect("Student_New.jsp?msg=success");
		}
	}

}
